package com.example.yiwei.crm.view.activity.customerActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.customerDao;
import com.example.yiwei.crm.data.GetContact;
import com.example.yiwei.crm.data.GetCustumer;
import com.example.yiwei.crm.dao_impl.customerDaoImpl;
import com.example.yiwei.crm.entity.ContactMessageEntity;
import com.example.yiwei.crm.entity.CustomerMessageEntity;

import java.util.List;

public class CustomerSearchActivity extends AppCompatActivity {
EditText edit_search;
    Button btn_search;
    ListView listview;
    MyAdapter adpter;
    boolean tag = false;
    Intent intent;
    List<CustomerMessageEntity> customerList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_search);
        edit_search= (EditText) findViewById(R.id.customer_edit_search);
        btn_search= (Button) findViewById(R.id.customer_btn_search);
        listview= (ListView) findViewById(R.id.customer_listview_search);
        adpter=new MyAdapter();
        listview.setDivider(new ColorDrawable(Color.rgb(216, 216, 216)));
        listview.setDividerHeight(1);
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        customerDao customerdao = new customerDaoImpl();
//                        customerList=customerdao.querySimpleCustomerMessage(baseClass.customer_search_url+edit_search.getText().toString(),null);
                        if ("kehuchi".equals(getIntent().getStringExtra("kehuchiTag"))) {
                            customerList = customerdao.querySimpleCustomerMessage(baseClass.another_customer_search_url+edit_search.getText().toString(), null);
                        } else {
                            customerList = customerdao.querySimpleCustomerMessage(baseClass.customer_search_url+edit_search.getText().toString(), null);
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (customerList.size()==0){
                                    Toast.makeText(CustomerSearchActivity.this, "客户不存在", Toast.LENGTH_SHORT).show();
                                }
                               listview.setAdapter(adpter);
                                adpter.notifyDataSetChanged();
                               listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                   @Override
                                   public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                       final int client_id = customerList.get(i).getClient_id();
                                        intent=new Intent(CustomerSearchActivity.this,CustomerDetailedMessageActivity.class);
                                       intent.putExtra("number", client_id);
                                       if ("kehuchi".equals(getIntent().getStringExtra("kehuchiTag"))) {
                                           intent.putExtra("kehuchiTag", "kehuchi");
                                           intent.putExtra("number", client_id);
                                       }
                                       new Thread(new Runnable() {
                                           @Override
                                           public void run() {
                                               customerDao customerdao = new customerDaoImpl();
                                               GetCustumer.customers = customerdao.queryDetailedCustomerMessage(baseClass.query_detailed_customer_message_url + client_id, null);
                                               if ("success".equals(GetCustumer.customers.getTag())) {
                                                   List<ContactMessageEntity> contacts = customerdao.queryContactAllMessage(baseClass.find_all_contact + client_id, null);
                                                   for (int i = 0; i < contacts.size(); i++) {
                                                       if (1 == contacts.get(i).getIs_first_contact()) {
                                                           GetContact.create();
                                                           GetContact.contact = contacts.get(i);
                                                           tag = true;
                                                       }
                                                   }
                                                   if (contacts.size() == 0 || !tag) {
                                                       GetContact.create();
                                                   }
                                                   tag = false;
                                                   startActivity(intent);
                                                   finish();
                                               } else {
                                                   runOnUiThread(new Runnable() {
                                                       @Override
                                                       public void run() {
                                                           Toast.makeText(CustomerSearchActivity.this, "数据加载失败", Toast.LENGTH_SHORT).show();
                                                       }
                                                   });
                                               }


                                           }
                                       }).start();

                                   }
                               });

                            }
                        });
                    }
                }).start();
            }
        });

    }
    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return customerList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {

            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.listview_item,null);
                holder = new ViewHolder();
                holder.item_text1 = (TextView) convertView.findViewById(R.id.listview_item_txt_name);
//                holder.item_text2= (TextView) convertView.findViewById(R.id.listview_item_txt_bz);
                holder.item_text3 = (TextView) convertView.findViewById(R.id.listview_item_createTime);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.item_text1.setText(customerList.get(position).getClient_name());
//            holder.item_text2.setText(customerList.get(position).getDisease_category());
//            if (customerList.get(position).getCreate_time()!=null){
//                SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
//                sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
//                String time = customerList.get(customerList.size()-1-position).getCreate_time();
//                long l = Long.parseLong(time);
//                Date date = new Date(l);
//                String create_time = sdf.format(date);
//                holder.item_text3.setText(create_time);
//            }
            return convertView;
        }
        class ViewHolder {
            TextView item_text1, item_text2, item_text3,item_text4;
        }
    }
}
